﻿using System;
using System.Linq;
using EFCTP5VSNH3.Model;
using System.Data.Entity;
using System.Configuration;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity.Validation;

namespace EntityFrameworkDemo
{

    public class Program
    {
        public static void Main(string[] args)
        {
            ////Custom database initializer.
            InitializeDatabase();

            Customer customer = new Customer()
            {
                CompanyName = "ENEL",
                Projects = new Collection<Project>(),
                CompanyAddress = new Adress()
                {
                    City = "ROMA",
                    Country = "ITALIA",
                    StreetAddress = "VIA DI TIZIO CAZIO, 17"
                }
            };

            Project prj1 = new Project
            {
                Description = "Data Repository Project"
            };

            Developer dev1 = new Developer
            {
                Name = "Pinco",
                Surname = "Pallino",
                Skills = new[]
                {
                    new Skill
                        {
                            Description = "C# 4.0", 
                            Rank = 4
                        },
                    new Skill
                        {
                            Description = "LINQ", 
                            Rank = 4
                        },
                    new Skill
                        {
                            Description = "Entity Framework", 
                            Rank = 4
                        }
                }
            };

            using (var db = new Db())
            {
                prj1.Customer = customer;
                customer.Projects.Add(prj1);

                try
                {
                    //Associate Developer and Project.
                    dev1.Projects = new List<Project>();
                    prj1.Developers = new List<Developer>();

                    dev1.Projects.Add(prj1);
                    prj1.Developers.Add(dev1);

                    // Add a new Developer
                    db.Developers.Add(dev1);

                    // Add a new Project.
                    db.Projects.Add(prj1);

                    var rowsAffected = db.SaveChanges();
                    //Console.WriteLine("Rows affected: {0}", rowsAffected);

                    var projectsCount = db.Projects.Select(p => p).Count();
                    var customersCount = db.Customers.Select(c => c).Count();
                    var developersCount = db.Developers.Select(d => d).Count();

                    Console.WriteLine("Developers in db: {0}", developersCount);
                    Console.WriteLine("Projects in db: {0}", projectsCount);
                    Console.WriteLine("Customers in db: {0}", customersCount);
                }
                catch (DbEntityValidationException ex)
                {
                    foreach (DbEntityValidationResult validationResult in ex.EntityValidationErrors)
                    {
                        Console.WriteLine("Error in " + validationResult.Entry.Entity);

                        foreach (DbValidationError validationError in validationResult.ValidationErrors)
                        {
                            Console.WriteLine("{0} : {1}", validationError.PropertyName, validationError.ErrorMessage);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            Console.ReadLine();
        }

        private static void InitializeDatabase()
        {
            Database.SetInitializer<Db>(new DbCustomInitializerStrategy<Db>());
        }
    }
}
